<template>
  <div class="app-container">
    <!-- 项目设置 -->
    <el-form ref="queryRef" :model="queryParams" :inline="true">
      <el-form-item label="项目类型" prop="projectId">
        <el-select v-model="queryParams.projectId" placeholder="请选择项目类型" @change="handleQuery">
          <el-option v-for="item in projectList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="项目名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col>
        <el-button type="primary" plain icon="Plus" @click="handleAdd">项目规则</el-button>
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">删除项目</el-button>
      </el-col>
    </el-row>

    <el-table :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目名称" prop="projectName" min-width="150" />
      <el-table-column label="营业时间" prop="businessStart" min-width="120" />
      <el-table-column label="闭店时间" prop="businessEnd" min-width="120" />
      <el-table-column label="是否允许半小时" min-width="150">
        <template #default="scope">
          <span>{{ scope.row.halfHourAllow == 1?'是':'否' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="150" class-name="small-padding">
        <template #default="scope">
          <el-button type="text" @click="updateRow(scope.row)">编辑</el-button>
          <el-button type="text" @click="viewRow(scope.row)">详情</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />

    <!-- 编辑 -->
    <dialogModifyProject :props="dialogModifyProps" :base-projects="projectList" @submitForm="resetQuery" />
    <!-- 详情 -->
    <dialogViewProject :props="dialogViewProps" />
  </div>
</template>

<script setup name="Project">
import { getCurrentInstance } from 'vue'
import { onMounted, reactive } from '@vue/runtime-core'
import { toRefs } from '@vueuse/shared'
import dialogModifyProject from './components/dialog/modifyProject'
import dialogViewProject from './components/dialog/viewProject'
import { delProjectConfig, projectConfigList } from '../../../../api/ven'
import { getBaseProjectList } from '../../../../api/oam/basic'


const { proxy } = getCurrentInstance()

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    status: undefined
  },
  loading: false,
  tableData: [],
  total: 0,
  multiple: true,
  projectList: []
})
const { queryParams, loading, tableData, total, multiple, projectList } = toRefs(data)

let selectedIds = []
const dialogPropsBase = {
  open: false,
  title: '',
  data: null
}
const dialogModifyProps = reactive({
  ...dialogPropsBase
})
const dialogViewProps = reactive({
  ...dialogPropsBase
})


// 获取列表
function getList() {
  loading.value = true
  projectConfigList(queryParams.value).then(res => {
    loading.value = false
    tableData.value = res.rows
    total.value = res.total
  })
}

function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

function handleSelectionChange(selection) {
  selectedIds = selection.map(item => item.id)
  multiple.value = !selection.length
}

function handleAdd() {
  _.assign(dialogModifyProps, dialogPropsBase, {
    open: true,
    title: '项目规则'
  })
}

function handleDelete(row) {
  let ids, title
  if (row.id) {
    ids = row.id
    title = `是否确认删除项目 ${row.projectName}`
  } else {
    ids = selectedIds
    title = `是否确认删除 ${selectedIds.length} 个项目？`
  }

  proxy.$modal.confirm(title).then(function() {
    return delProjectConfig(ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess('删除成功')
  })
}

function updateRow(row) {
  _.assign(dialogModifyProps, dialogPropsBase, {
    open: true,
    title: '编辑规则',
    data: { ...row }
  })
}

function viewRow(row) {
  _.assign(dialogViewProps, dialogPropsBase, {
    open: true,
    data: { ...row }
  })
}


function getProjectList() {
  getBaseProjectList().then(res => {
    projectList.value = res.rows
  })
}

onMounted(() => {
  getList()
  getProjectList()
})

</script>

<style scoped></style>
